//
//  SNKDataBase.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/26.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKDataBase.h"
#import <FMDB.h>

#import "CollectModel.h"              //新闻收藏model
#import "PhotoCollectModel.h"         //图片收藏model
#import "SNKVideoCollectModel.h"      //视频收藏model

@implementation SNKDataBase

static FMDatabaseQueue *_queue;

#pragma mark - 初始化，创建表
+ (void)initialize {
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"SleepNews.sqlite"];
    
    // 创建队列
    _queue = [FMDatabaseQueue databaseQueueWithPath:path];
    
    // 创建新闻收藏表
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"create table if not exists t_news (id integer primary key autoincrement, title text,docid text,time text);"];
    }];
    // 创建图片收藏表
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"create table if not exists t_photos (id integer primary key autoincrement, title text,image_url text,image_width real,image_height real,time text);"];
    }];
    // 创建视频收藏表
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"create table if not exists t_videos (id integer primary key autoincrement, title text,image_url text,video_url text,time text);"];
    }];
}

#pragma mark 存数据
+ (void)addNewsWithTitle:(NSString *)title docid:(NSString *)docid time:(NSString *)time {
    [_queue inDatabase:^(FMDatabase *db) {
        // 存新闻
        [db executeUpdate:@"insert into t_news (title,docid,time) values(?,?,?)",title,docid,time];
    }];
}

+ (void)addPhotosWithTitle:(NSString *)title image_url:(NSString *)image_url image_width:(NSString *)image_width image_height:(NSString *)image_height time:(NSString *)time {
    [_queue inDatabase:^(FMDatabase *db) {
        // 存图片
        [db executeUpdate:@"insert into t_photos (title,image_url,image_width,image_height,time) values(?,?,?,?,?)",title,image_url,image_width,image_height,time];
    }];
}

+ (void)addVideosWithTitle:(NSString *)title image_url:(NSString *)image_url video_url:(NSString *)video_url time:(NSString *)time {
    [_queue inDatabase:^(FMDatabase *db) {
        // 存视频
        [db executeUpdate:@"insert into t_videos (title,image_url,video_url,time) values(?,?,?,?)",title,image_url,video_url,time];
    }];
}

#pragma mark 显示数据

// 显示新闻
+ (NSMutableArray *)newsDisplay {
    __block NSMutableArray *dicArray = nil;
    [_queue inDatabase:^(FMDatabase *db) {
        
        dicArray = [NSMutableArray array];
        // 1.查询数据
        FMResultSet *rs = [db executeQuery:@"select * from t_news"];
        
        // 2.遍历结果集
        while (rs.next) {
            NSString *title = [rs stringForColumn:@"title"];
            NSString *docid = [rs stringForColumn:@"docid"];
            NSString *time = [rs stringForColumn:@"time"];
            
            CollectModel *collectmodel = [[CollectModel alloc]init];
            collectmodel.title = title;
            collectmodel.docid = docid;
            collectmodel.time = time;
            
            [dicArray addObject:collectmodel];
        }
    }];
    return dicArray;
}

// 显示图片
+ (NSMutableArray *)photosDisplay {
    __block NSMutableArray *dicArray = nil;
    [_queue inDatabase:^(FMDatabase *db) {
        
        dicArray = [NSMutableArray array];
        // 1.查询数据
        FMResultSet *rs = [db executeQuery:@"select * from t_photos"];
        
        // 2.遍历结果集
        while (rs.next) {
            NSString *title = [rs stringForColumn:@"title"];
            NSString *image_url = [rs stringForColumn:@"image_url"];
            CGFloat image_width = [[rs stringForColumn:@"image_width"] floatValue];
            CGFloat image_height = [[rs stringForColumn:@"image_height"] floatValue];
            NSString *time = [rs stringForColumn:@"time"];
            
            PhotoCollectModel *photomodel = [[PhotoCollectModel alloc] init];
            photomodel.title = title;
            photomodel.image_url = image_url;
            photomodel.image_width = image_width;
            photomodel.image_height = image_height;
            photomodel.time = time;
            
            [dicArray addObject:photomodel];
        }
    }];
    return dicArray;
}

// 显示视频
+ (NSMutableArray *)videosDisplay {
    __block NSMutableArray *dicArray = nil;
    [_queue inDatabase:^(FMDatabase *db) {
        
        dicArray = [NSMutableArray array];
        // 1.查询数据
        FMResultSet *rs = [db executeQuery:@"select * from t_videos"];
        
        // 2.遍历结果集
        while (rs.next) {
            NSString *title = [rs stringForColumn:@"title"];
            NSString *image_url = [rs stringForColumn:@"image_url"];
            NSString *video_url = [rs stringForColumn:@"video_url"];
            NSString *time = [rs stringForColumn:@"time"];
            
            SNKVideoCollectModel *videoModel = [[SNKVideoCollectModel alloc] init];
            videoModel.title = title;
            videoModel.image_url = image_url;
            videoModel.video_url = video_url;
            videoModel.time = time;
            
            [dicArray addObject:videoModel];
        }
    }];
    return dicArray;
}

+ (NSString *)queryNewsCollectWithDocid:(NSString *)docid {
    __block NSString *str = @"0";
    [_queue inDatabase:^(FMDatabase *db) {
        FMResultSet *rs = [db executeQuery:@"select * from t_news where docid = ?;",docid];
        while (rs.next) {
            str = @"1";
        }
    }];
    return str;
}

+ (NSString *)queryPhotoCollectWithImage_url:(NSString *)image_url {
    __block NSString *str = @"0";
    [_queue inDatabase:^(FMDatabase *db) {
        FMResultSet *rs = [db executeQuery:@"select * from t_photos where image_url = ?;",image_url];
        while (rs.next) {
            str = @"1";
        }
    }];
    return str;
}

+ (NSString *)queryVideosCollectWithImage_url:(NSString *)image_url {
    __block NSString *str = @"0";
    [_queue inDatabase:^(FMDatabase *db) {
        FMResultSet *rs = [db executeQuery:@"select * from t_videos where image_url = ?;",image_url];
        while (rs.next) {
            str = @"1";
        }
    }];
    return str;
}

#pragma mark -  删除表
+ (void)deleteNewsTable {
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_news"];
    }];
}

+ (void)deletePhotosTable {
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_photos"];
    }];
}

+ (void)deleteVideosTable {
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_videos"];
    }];
}

#pragma mark -  删除单条数据
+ (void)deleteNewsTableWithDocid:(NSString *)docid {
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_news where docid = ?;",docid];
    }];
}

+ (void)deletePhotosTableWithImage_url:(NSString *)image_url {
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_photos where image_url = ?;",image_url];
    }];
}

+ (void)deleteVideosTableWithImage_url:(NSString *)image_url {
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_videos where image_url = ?;",image_url];
    }];
}



@end
